<template>
  <el-dialog
    title="已领取用户"
    v-model="visible"
    width="850px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="用户名称" prop="clientName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.clientName"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="领取时间" prop="time">
          <el-date-picker
            style="width: 240px"
            v-model="queryParams.time"
            type="datetimerange"
            format="YYYY-MM-DD HH:mm"
            value-format="YYYY-MM-DD HH:mm"
            date-format="YYYY-MM-DD"
            time-format="HH:mm"
            :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" size="default" :data="dataList">
        <el-table-column label="用户" align="center" prop="clientName" />
        <el-table-column label="领取时间" align="center" prop="receiveTime" />
        <el-table-column label="领取优惠券数" align="center" prop="count" />
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { getAlreadyReceivedListApi } from '@/api/marketing/giftPack'
const queryRef = ref(null)

const total = ref(0)
const dataList = ref([])
const visible = ref(false)

const queryParams = reactive({
  id: '',
  clientName: '',
  receiveTimeStart: '',
  receiveTimeEnd: '',
  time: '',
  pageNum: 1,
  pageSize: 10
})
const loading = ref(true)

/** 查询用户列表 */
function getList() {
  loading.value = true
  const params = {
    ...queryParams,
    receiveTimeStart: queryParams.time?.length ? queryParams.time[0] : '',
    receiveTimeEnd: queryParams.time?.length ? queryParams.time[1] : ''
  }
  delete params.time
  getAlreadyReceivedListApi(params)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
  queryParams.pageSize = 10
}

function init(id) {
  visible.value = true
  queryParams.id = id
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.main {
  position: relative;
  transform: translate3d(0, -30px, 0);
}
.type {
  margin-bottom: 20px;
}
</style>
